package org.xamp.gui.xmpp;

import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;

public class Connection {

	public Connection(String server) {
		xmppConnection = new XMPPConnection(server);
		contactList = new ContactList(xmppConnection);
	}

	/**
	 * 
	 */
	private XMPPConnection xmppConnection;
	
	private ContactList contactList;

	public XMPPConnection getXmppConnection() {
		return xmppConnection;
	}
	
	public ContactList getContactList() {
		return contactList;
	}

	/**
	 * 
	 * @param server
	 * @return
	 */
	public boolean connect() {
		try {
			xmppConnection.connect();
		} catch (XMPPException xmppException) {
			xmppException.printStackTrace();
		}
		return xmppConnection.isConnected();
	}
	
	public boolean disconnect() {
		xmppConnection.disconnect();
		return xmppConnection.isConnected();
	}

	/**
	 * 
	 * @param username
	 * @param password
	 * @param resource
	 * @return
	 */
	public boolean login(String username, String password, String resource) {
		SASLAuthentication.supportSASLMechanism("PLAIN", 0);
		try {
			if (resource != null) {
				xmppConnection.login(username, password, resource);
			} else {
				xmppConnection.login(username, password);
			}
		} catch (XMPPException xmppException) {
			xmppException.printStackTrace();
		}
		return xmppConnection.isAuthenticated();
	}

}
